package
{
	import com.demonsters.debugger.MonsterDebugger;
	import com.greensock.events.LoaderEvent;
	import com.rover022.flixelgame.GameConfig;
	import com.rover022.flixelgame.GameEngine;
	import com.rover022.flixelgame.manager.AssetManager;
	import com.rover022.flixelgame.ui.debug.DebugSprite;
	import com.rover022.flixelgame.world.CityState;
	import flash.display.Sprite;
	import flash.events.Event;
	import ghostcat.manager.RootManager;

	[SWF(width = "1024", height = "600", frameRate = "45")]
	public class FlixelGame extends Sprite
	{
		public var appLay:Sprite;
		public var uiLay:Sprite;
		public var debugLay:Sprite;
		public var gamemain:GameEngine;
		private var _herosConfigID:String = "xml/hero.xml";
		private var _battleConfigID:String = "xml/Battleinfo.xml";
		private var _itemConfigID:String = "xml/item.xml";
		
		private var _gameConfigID:String = "xml/GameConfig.xml";
		private var _gameDeveloper:String = "FGame游戏开发版本v1.0";
		private var _gameContact:String = "联系作者QQ:9234164";

		public function FlixelGame()
		{
			//add bug tool
			MonsterDebugger.initialize(this);
			//add manger
			RootManager.register(this);
			RootManager.addURLMenu(_gameDeveloper);
			RootManager.addURLMenu(_gameContact);
			addEventListener(Event.ADDED_TO_STAGE, initConfig);
		}

		/**
		 * 加载配置文件
		 * @param event
		 *
		 */
		protected function initConfig(event:Event):void
		{
			//目前没有配置文件直接初始化游戏
			var configArray:Array = [];
			configArray.push(_herosConfigID);
			configArray.push(_battleConfigID);
			configArray.push(_gameConfigID);
			configArray.push(_itemConfigID);
			AssetManager.getInstance().loadAssetArray(configArray, configLoadComplete);
		}

		private function configLoadComplete():void
		{
			GameConfig.heroData = AssetManager.getInstance().requestAsset(_herosConfigID);
			GameConfig.gameData = AssetManager.getInstance().requestAsset(_gameConfigID);
			GameConfig.itemData = AssetManager.getInstance().requestAsset(_itemConfigID);
			
			initGame();
		}

		private function initGame():void
		{
			appLay = makeLay("appLay");
			uiLay = makeLay("uiLay")
			gamemain = new GameEngine(1024, 625, CityState);
			appLay.addChild(gamemain);
			//如果是debug模式
			initDebugMode();
		}

		/**
		 * 制造相关的图层丢在场景上
		 * @param param0
		 * @return
		 *
		 */
		private function makeLay(_name:String):Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.name = _name;
			addChild(sprite);
			RootManager.setValue(_name, sprite);
			return sprite;
		}

		private function initDebugMode():void
		{
			debugLay = new DebugSprite();
			addChild(debugLay);
		}
	}
}
